package com.zx.main;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
 
public class Window extends JFrame implements ActionListener {
     
    private static final long serialVersionUID = 1L;
     
    private static JButton open=null;
    
    private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     
    public Window(){
        open=new JButton("日志分析");
        this.add(open);
        this.setBounds(400, 200, 100, 100);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        open.addActionListener(this);
         
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser jfc=new JFileChooser();
        // 目录和文件都可以选择
//      jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
        jfc.showDialog(new JLabel(), "选择");
        //只能选择文件
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 自定义过滤器
       // FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
        //jfc.setFileFilter(filter);
        jfc.setFileFilter(new FileFilter() {
            // 设置文件格式
            private String description = "log,txt";
             
            @Override
            public String getDescription() {
                return description;
            }
             
            @Override
            public boolean accept(File f) {
                if(f != null){
                    if(f.isDirectory()){
                        return false ;
                    }
                    String extension  = getExtension(f) ;
                    if(extension != null && this.description.toLowerCase().contains(extension))
                        return true ;
                }
                return false;
            }
             
            private String getExtension(File f) {
                if (f != null) {
                    String filename = f.getName();
                    int i = filename.lastIndexOf('.');
                    if (i > 0 && i < filename.length() - 1) {
                        return filename.substring(i + 1).toLowerCase();
                    }
                }
                return null;
            }
        }) ;
        File file=jfc.getSelectedFile();
        if(file != null){
            showFileContent(file) ;
        } else {
            System.out.println("不支持的文件类型");
        }
    }
 
    /**
     * 用于显示文件内容
     * @param file
     */
    public void showFileContent(File file){
        JFrame jFrame = new JFrame(file.getAbsolutePath()) ;
        BufferedReader br = null ;
        JTextArea jta= new JTextArea() ;
        try{
            br = new BufferedReader(new FileReader(file)) ;
            String tmp = null ;
            while((tmp = br.readLine()) != null){
                jta.setText(jta.getText() + "\r\n" + tmp) ;
            }
            br.close() ;
        } catch (IOException e) {
            e.printStackTrace() ;
        }
         
        JScrollPane scrollPane = new JScrollPane(jta);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
         
        jFrame.add(scrollPane, BorderLayout.CENTER) ;
        jFrame.setBounds(0, 0, screenSize.width , screenSize.height ) ;
        jFrame.setVisible(true);
    }
    
 
}